package com.lfz.manage.service;

import com.lfz.common.exception.MsgException;
import com.lfz.manage.mapper.JobMapper;
import com.lfz.manage.pojo.Company;
import com.lfz.manage.pojo.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class JobService {

    @Autowired
    private JobMapper mapper;

    public List<Job> listAll() {
        Job job = new Job();
        job.setStatus(Job.STATUS_OK);
        return mapper.select(job);
    }

    public Job getJobById(String jobId) {
        Job job = new Job();
        job.setJobId(jobId);
        job.setStatus(Job.STATUS_OK);
        List<Job> list = mapper.select(job);
        if (list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    public void addJob(Job job) throws MsgException {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        job.setCreateBy(timestamp);
        job.setUpdateBy(timestamp);
        job.setStatus(Job.STATUS_OK);
        int rowaffec = mapper.insert(job);
        if (rowaffec != 1) {
            throw new MsgException("添加失败！");
        }
    }

    public void updateJob(Job job) throws MsgException {
        job.setUpdateBy(new Timestamp(System.currentTimeMillis()));
        int rowaffec = mapper.updateByPrimaryKeySelective(job);
        if (rowaffec != 1) {
            throw new MsgException("记录不存在！");
        }
    }

    public void deleteJob(String jobId) throws MsgException {
        Job job = new Job();
        job.setJobId(jobId);
        job.setStatus(Company.STATUS_DELETE);
        job.setUpdateBy(new Timestamp(System.currentTimeMillis()));
        int rowAffec = mapper.updateByPrimaryKeySelective(job);
        if (rowAffec != 1) {
            throw new MsgException("记录不存在！");
        }
    }
}
